<?php
	session_start(); 
	require('./include/phpsqlajax_dbinfo.php');
	
	function parseToKML($htmlStr){
		$kmlStr=str_replace('&','&amp;',$htmlStr);
		return $kmlStr; 
	}
	
	 // Opens a connection to a MySQL server.
	$connection = mysql_connect ($server, $username, $password);
	if (!$connection) 
	{
	  die('Not connected : ' . mysql_error());
	}
	
	// Sets the active MySQL database.
	$db_selected = mysql_select_db($database, $connection);
	if (!$db_selected) 
	{
	  die ('Can\'t use db : ' . mysql_error());
	}
	
	 //Set result charset to UTF-8
	 mysql_query('SET NAMES "utf8"');
 
	 if($_POST['name']!='' && $_POST['town']!='' && $_POST['plz']!=''){
	 /*
	 *geocode adress if not in $_POST
	 */
		if($_POST['lon']==0 && $_POST['lat']==0){
			$address1=$_POST['street']." ".$_POST['nr']." ".$_POST['town']." ".$_POST['plz'];
			$url='http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address='.urlencode($address1).'';
			$neuxml = simplexml_load_file($url) or die("url not loading");
			$status = $neuxml->status;
			if ($status=="OK") {
				$Lat = $neuxml->result->geometry->location->lat;
				$Lon = $neuxml->result->geometry->location->lng;
				$_POST['lat']=$Lat;
				$_POST['lon']=$Lon;
			}
		}


		
		/*
		*insert event
		*/	
			$insertEvents = 'INSERT INTO `events`(`Users_idUsers`, `lat`, `long`, `name`, `description`, `fee`, `date`, `visibility`, `town`, `plz`, `street`, `nr`, `Contacts_idContacts`) 
				VALUES ("'.$_POST['culturalUser'].'","'.$_POST['lat'].'","'.$_POST['lon'].'","'.$_POST['name'].'","'.$_POST['description'].'",
				"'.$_POST['fee'].'","'.$_POST['date'].'","'.$_POST['visibility'].'","'.$_POST['town'].'","'.$_POST['plz'].'",
				"'.$_POST['street'].'","'.$_POST['nr'].'",null)';
			mysql_query($insertEvents)or die (mysql_error());
			$eventId = mysql_insert_id();	
		
		
		/*
		*insert data in Events has categories
		*/
		$eventCategories = explode(", ",$_POST['eventCategory']);
		for($i=0;$i<count($eventCategories);$i++){
		
		$getEventCategory='SELECT `idEventCategories` FROM `eventcategories` WHERE `eventcategory`="'.$eventCategories[$i].'"';
		$resEventCategory = mysql_query($getEventCategory)or die;
		$eventCategoryRow=mysql_fetch_row($resEventCategory);
		$insertEventCategory='INSERT INTO `events_has_eventcategories`(`Events_idEvents`, `EventCategories_idEventCategories`) VALUES ("'.$eventId.'","'.$eventCategoryRow[0].'")';
		mysql_query($insertEventCategory)or die (mysql_error());
		}
		
		
		/*
		*Insert Keyword
		*/		
		if($_POST['keyword']!=''){
			$checkKeyword='SELECT COUNT(idKeywords),`idKeywords` FROM `keywords` WHERE `keyword`="'.$_POST['keyword'].'"';
			$resKeyword = mysql_query($checkKeyword)or die;
			$keywordRow=mysql_fetch_row($resKeyword);
			if($keywordRow[0]==0){//test whether keyword already exist
				$insertNewKeyword='INSERT INTO `keywords`(`keyword`) VALUES ("'.$_POST['keyword'].'")';
				mysql_query($insertNewKeyword)or die;
				$keywordId = mysql_insert_id();			
				$insertNewEventKeyword='INSERT INTO `keywords_has_events`(`Keywords_idKeywords`, `Events_idEvents`) VALUES ("'.$keywordId.'","'.$eventId.'")';
				mysql_query($insertNewEventKeyword)or die;
			}else{
				$insertEventKeyword='INSERT INTO `keywords_has_events`(`Keywords_idKeywords`, `Events_idEvents`) VALUES ("'.$keywordRow[1].'","'.$eventId.'")';
				mysql_query($insertEventKeyword)or die;
			}
		}
		/*
		*insert Links
		*/		
		if($_POST['link']!=''){
		
			$insertNewEventLink='INSERT INTO `links`(`link`, `Events_idEvents`, `Users_idUsers`) VALUES ("'.$_POST['link'].'","'.$eventId.'",null)';
			mysql_query($insertNewEventLink)or die;
			$linkId = mysql_insert_id();			
			
		}
		
		/*
		*Idee zum Fileupload
		*/
		/*if (isset($_FILES['attachment']) and ! $_FILES['attachment']['error'] and $_FILES['attachment']['size']<2097152)  {
			if($_FILES['attachment']['type']=='image/gif'){
				move_uploaded_file($_FILES['attachment']['tmp_name'], "../img/data/".$eventId.".gif");
				$insertDataGif='INSERT INTO `attachments`(`directory`, `Events_idEvents`) VALUES ("'.$eventId.'".gif","'.$eventId.'")';
				mysql_query ( $insertDataGif ) or die;
				$dataId = mysql_insert_id();
			}
				
		}*/
	
	}

?>